<?php

namespace Api\Event;

// +----------------------------------------------------------------------
// | 文件 UserNoticeEvent.class.php
// +----------------------------------------------------------------------
// | 说明 用户消息操作
// +----------------------------------------------------------------------
// | 
// +----------------------------------------------------------------------
// | Author: 谢赤心 Legend. <xcx_legender@qq.com>
// +----------------------------------------------------------------------

class UserNoticeEvent extends BaseEvent 
{   

    // 获取数据
    public function getLists($uid, $p = 1, $pager = 10)
    {
        $model = service('UserNotice');
        $map['uid'] = (int)$uid;
        return $model->where($map)->order('id desc')->page($p)->limit($pager)->select();
         // echo $model->_sql();
    }

    // 增加视频点赞消息
    public function addVideoZan(&$senderUser, $toUid, &$video, $content)
    {
        $type = 'video_zan';
        $save = [
            'type' => $type,
            'uid'  => (int)$toUid,
            'content' => $content,
            'data' => [
                'uid' => (int)$senderUser['uid'],
                'nickname' => $senderUser['nickname'],
                'target_id' => (int)$video['id']
            ],
        ];
        $model = service('UserNotice');
        $model->create($save) && $model->add();
        // \Think\Log::record("[DEBUG: -- NOTICE --]".json_encode($save), 'DEBUG');
        // $save && $model->add($save);
    }

    // TODO 添加视频评论的通知内容
    public function addComment(&$senderUser, $toUid, &$video, $content)
    {
        $type = 'video_comment';
        $save = [
            'type' => $type,
            'uid'  => (int)$toUid,
            'content' => $content,
            'data' => [
                'uid' => (int)$senderUser['uid'],
                'nickname' => $senderUser['nickname'],
                'target_id' => (int)$video['id']
            ],
        ];
        $model = service('UserNotice');
        $model->create($save) && $model->add();
    }

    // 增加用户点赞消息
    public function addLike(&$senderUser, $toUid, $content)
    {
        $type = 'like';
        $save = [
            'type' => $type,
            'uid'  => (int)$toUid,
            'content' => $content,
            'data' => [
                'uid' => (int)$senderUser['uid'],
                'nickname' => $senderUser['nickname']
            ],
        ];
        $model = service('UserNotice');
        $model->create($save) && $model->add();
    }

    // 增加关注消息
    public function addFans(&$senderUser, $toUid, $content)
    {
        $type = 'fans';
        $save = [
            'type' => $type,
            'uid'  => (int)$toUid,
            'content' => $content,
            'data' => [
                'uid' => (int)$senderUser['uid'],
                'nickname' => $senderUser['nickname']
            ],
        ];
        $model = service('UserNotice');
        $model->create($save) && $model->add();
    }


    // 添加系统消息
    public function addSystem($toUid, $content, $extend = '')
    {
        $type = 'system';
         $save = [
            'type' => $type,
            'uid'  => (int)$toUid,
            'content' => '[系统消息]'.$content,
            'data' => $extend
        ];
        $model = service('UserNotice');
        $model->create($save) && $model->add();
    }

    // 增加用户打赏消息
    public function addReward(&$senderUser, $toUid, $content)
    {
        $type = 'reward';
        $save = [
            'type' => $type,
            'uid'  => (int)$toUid,
            'content' => $content,
            'data' => [
                'uid' => (int)$senderUser['uid'],
                'nickname' => $senderUser['nickname']
            ],
        ];
        $model = service('UserNotice');
        $model->create($save) && $model->add();
    }

}